package org.neuedu.servlet;

import org.neuedu.bean.User;
import org.neuedu.service.CollectService;
import org.neuedu.service.CollectServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet(name = "CollectArticleServlet",urlPatterns = "/collect")
public class CollectArticleServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String aid = request.getParameter("aid");
        HttpSession session = request.getSession();
        User user = (User)session.getAttribute("fly_user");
        Integer uid = user.getId();
        CollectService service = new CollectServiceImpl();
        int count = service.serchCollectArticleByAid(Integer.valueOf(aid));
        if(count == 0){
            int i = service.updateCollectArticle(Integer.valueOf(aid),uid);
            if (i == 1){
                request.getRequestDispatcher("/article/detail").forward(request,response);
            }else {
                request.setAttribute("collect_fail","收藏失败");
                request.getRequestDispatcher("/article/detail").forward(request,response);
            }
        }else {
            request.setAttribute("collect_fail","您已收藏过此篇文章");
            request.getRequestDispatcher("/article/detail").forward(request,response);
        }
    }
}
